package com.scs.application.modules.dept.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.dept.entity.LimitMatr;
import com.scs.application.modules.dept.mapper.LimitMatrMapper;
import com.scs.application.modules.dept.service.LimitMatrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Iterator;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class LimitMatrServiceImpl extends BaseServiceImpl<LimitMatrMapper, LimitMatr> implements LimitMatrService {

    @Autowired
    com.scs.application.modules.base.service.MatrService matrService;

    @Override
    public boolean saveOrUpdateBatch(Collection<LimitMatr> entityList) {
        Iterator<LimitMatr> iterator = entityList.iterator();
        while (iterator.hasNext()) {
            LimitMatr item = iterator.next();
            baseMapper.delete(new QueryWrapper<LimitMatr>()
                    .eq("is_apply", GlobalConsts.NO)
                    .eq("is_demand", GlobalConsts.NO));
            Integer selectCount = baseMapper.selectCount(new QueryWrapper<LimitMatr>()
                    .eq("matr_id", item.getMatrId())
                    .eq("dept_id", item.getDeptId()));
            if (selectCount > 0) {
                iterator.remove();
            }
        }
        if (entityList.isEmpty()) {
            throw new BusinessException("选择的耗材在科室耗材范围已存在,请重新选择");
        }
        return super.saveOrUpdateBatch(entityList);
    }


}
